package com.woniuxy.stage3demo.service.chenyxuanService.serviceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.stage3demo.bean.chenyuxuanBean.DeptBean;
import com.woniuxy.stage3demo.pageHelper.PageRequest;
import com.woniuxy.stage3demo.bean.chenyuxuanBean.StaffBean;
import com.woniuxy.stage3demo.mapper.chenyuxuanMapper.StaffMapper;
import com.woniuxy.stage3demo.service.chenyxuanService.IStaffService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional(readOnly = true)
public class StaffServiceImpl implements IStaffService {

    @Resource
    private StaffMapper staffMapper;

    /**
     * 添加员工信息
     *
     * @param staffBean
     * @return
     */
    @Transactional(isolation = Isolation.REPEATABLE_READ, rollbackFor = Exception.class)
    @Override
    public int addStaff(StaffBean staffBean, DeptBean deptBean) {

        return staffMapper.addStaff(staffBean, deptBean);
    }

    /**
     * 添加员工信息
     * @param staffBean
     * @return
     */
    @Transactional(isolation = Isolation.REPEATABLE_READ, rollbackFor = Exception.class)
    @Override
    public int addStaffInDept(StaffBean staffBean) {
        return staffMapper.addStaffInDept(staffBean);
    }

    /**
     * 根据ID删除员工,改变del_flag标记为1
     * @param id
     */
    @Transactional(isolation = Isolation.REPEATABLE_READ, rollbackFor = Exception.class)
    @Override
    public int deleteStaff(int id) {
       return staffMapper.deleteStaff(id);
    }

    /**
     * 根据ID查询员工具体信息
     *
     * @param id
     */
    @Override
    public StaffBean findStaffById(int id) {

        return staffMapper.findStaffById(id);
    }

    /**
     * 分页查询所有员工
     *
     * @param request   分页条件请求参数
     * @param staffBean 员工信息
     * @return
     */
    @Override
    public PageInfo<StaffBean> findAllByParams(PageRequest request, StaffBean staffBean) {
        return PageHelper.startPage(request.getPageNum(), request.getLimit()).
                doSelectPageInfo(() -> staffMapper.findAllByParams(request, staffBean));
    }


    /**
     * 修改员工信息
     *
     * @param staffBean
     */
    @Transactional(isolation = Isolation.REPEATABLE_READ, rollbackFor = Exception.class)
    @Override
    public int updateStaff(StaffBean staffBean) {
       return staffMapper.updateStaff(staffBean);
    }


    /**
     * 根据部门id查询部门所有人数
     * @param id
     * @return
     */
   /* @Override
    public int countDeptStaffnum(int id) {
        return staffMapper.countDeptStaffnum(id);
    }*/


    /**
     * 查询某部门所有的成员
     * @param id
     * @return
     */
    @Override
    public List<StaffBean> findAllStaffByDeptId(int id) {
        return staffMapper.findAllStaffByDeptId(id);
    }


}
